﻿package systems.traer 
{
	/**
	 * ...
	 * @author tlecoz
	 */
	public class ModifiedEulerIntegrator implements Integrator
	{
		
		private var s:ParticleSystem;
		
		public function ModifiedEulerIntegrator(_s:ParticleSystem) 
		{
			s = _s;
		}
		public function step(t:Number):void {
			s.clearForces();
			s.applyForces();
			
			var halftt:Number = 0.5*t*t;
			var i:int;
			var len:int = s.numberOfParticles();
			var p:Particle;
			var ax:Number,ay:Number,az:Number
			for ( i = 0; i <len; i++ )
			{
				p = s.getParticleById( i ) as Particle;
				if ( p.isFree() )
				{
					ax = p.getForce().x/p.getMass();
					ay = p.getForce().y/p.getMass();
					az = p.getForce().z/p.getMass();
					
					p.getPosition().addFromValues( p.getVelocity().x/t, p.getVelocity().y/t, p.getVelocity().z/t );
					p.getPosition().addFromValues( ax*halftt, ay*halftt, az*halftt );
					p.getPosition().addFromValues( ax/t, ay/t, az/t );
				}
			}
			
		}
		
	}

}